﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using KC.Business.Net;
using utils = KC.Business.Net.utils;

namespace KC.Tests
{
	[TestClass]
	public class Net
	{
		[TestMethod]
		public void isSearchEngineReferralTest()
		{
			string google = "http://www.google.com/search?hl=en&source=hp&q=w00terz&aq=f&oq=&aqi=g7g-s1g2";
			string yahoo = "http://search.yahoo.com/search?p=yarrrrrgh&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-701";
			string bing = "http://www.bing.com/search?q=mwuhahahaha&form=QBLH&qs=n";
			string aol = "http://search.aol.com/aol/search?s_it=comsearch40bt1&query=AOL+fracking+sucks";
			string baidu = "http://www.baidu.com/s?wd=wierd+chinese+shiznit";

			string Engine, Query;
			Assert.IsTrue(utils.isSearchEngineReferral(new Uri(google), out Engine, out Query));
			Assert.AreEqual("google", Engine);
			Assert.AreEqual("w00terz", Query);
			Assert.IsTrue(utils.isSearchEngineReferral(new Uri(yahoo), out Engine, out Query));
			Assert.AreEqual("yahoo", Engine);
			Assert.AreEqual("yarrrrrgh", Query);
			Assert.IsTrue(utils.isSearchEngineReferral(new Uri(bing), out Engine, out Query));
			Assert.AreEqual("bing", Engine);
			Assert.AreEqual("mwuhahahaha", Query);
			Assert.IsTrue(utils.isSearchEngineReferral(new Uri(aol), out Engine, out Query));
			Assert.AreEqual("aol", Engine);
			Assert.AreEqual("AOL fracking sucks", Query);
			Assert.IsTrue(utils.isSearchEngineReferral(new Uri(baidu), out Engine, out Query));
			Assert.AreEqual("baidu", Engine);
			Assert.AreEqual("wierd chinese shiznit", Query);
		}
		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		public void isSearchEngineReferralNullTest()
		{
			string Engine, Query;
			utils.isSearchEngineReferral(null, out Engine, out Query);
		}
	}
	[TestClass]
	public class NetSpiderTests
	{
		#region Constants
		private const string c_IE8_UserAgent = @"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)";
		private readonly string[] c_SpiderStrings = new string[] {
			"66.249.73.7",
			"66.249.71.194",
			"66.249.71.193",
			"66.249.16.212",
			"66.249.65.71",
			"66.249.16.211",
			"66.249.71.39",
			"66.249.70.188",
			"66.249.70.56",
			"Googlebot",
			"Yahoo! Slurp" };
		private readonly string[] c_NonSpiderStrings = new string[] {
			"75.70.109.79",
			"206.72.124.93"
		};
		#endregion Constants

		[TestMethod]
		public void IsSpiderTrueTest()
		{
			foreach (string str in c_SpiderStrings)
				Assert.IsTrue(utils.isSpider(c_IE8_UserAgent, str), "Failed on string: " + str);
		}

		[TestMethod]
		public void IsSpiderFalseTest()
		{
			foreach (string str in c_NonSpiderStrings)
			{
				Assert.IsFalse(utils.isSpider(null, str), "Failed on string: " + str);
			}
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException), "Unable to detect spider, this is an invalid Request with no IP address")]
		public void IsSpiderNullTest()
		{
			utils.isSpider(c_IE8_UserAgent, null);
		}

		[TestMethod]
		public void IsSpiderNull2Test()
		{
			Assert.IsFalse(utils.isSpider(null, "127.0.0.1"));
		}

	}
}
